package com.zl.learning.io.test.com.ronsoft.books.nio.regex;

/**
 * Created by zhaolei on 2017/4/26.
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Demonstrate behavior of backslashes in regex patterns.
 *
 171
 * @author Ron Hitchens (ron@ronsoft.com)
 */
public class BackSlashes
{
    public static void main (String [] argv)
    {
// 在输入中把“a\b”替换成XYZ或ABC
        String rep = "a\\\\b";
        String input = "> XYZ <=> ABC <";
        Pattern pattern = Pattern.compile ("ABC|XYZ");
        Matcher matcher = pattern.matcher (input);
        System.out.println (matcher.replaceFirst (rep));
        System.out.println (matcher.replaceAll (rep));
// 在输入中更改所有的新行来转义， DOS-like CR/LF
        rep = "\\\\r\\\\n";
        input = "line 1\nline 2\nline 3\n";
        pattern = Pattern.compile ("\\n");
        matcher = pattern.matcher (input);
        System.out.println ("");
        System.out.println ("Before:");
        System.out.println (input);
        System.out.println ("After (dos-ified, escaped):");
        System.out.println (matcher.replaceAll (rep));
    }
}
